//
//  SceneDelegate.swift
//  test-ios
//
//  Created by wyz5204 on 2023/2/2.
//

import UIKit
import CocoaLumberjack

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?
    
    public static var shared : SceneDelegate {
        get{
            let scene = UIApplication.shared.connectedScenes.first
            return scene?.delegate as! SceneDelegate
        }
    }
    
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        self.window = UIWindow.init(windowScene: windowScene)
        self.window?.frame = windowScene.coordinateSpace.bounds
        
        self.window?.backgroundColor = UIColor.white
        
        self.window?.rootViewController = UINavigationController(rootViewController: SplashViewController())
        
        self.window?.makeKeyAndVisible()
        
        // 进行消息注册 包括登陆成功，退出成功
        // 登陆成功，发送通知，自动跳转到MainViewController
        // 退出成功，发送通知，自动跳转到LoginViewCOmtroller
        
        // RD
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccess), name: RDNotification.login, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(quitSuccess), name: RDNotification.quit, object: nil)
    }
    
    func sceneDidDisconnect(_ scene: UIScene) {
        //print("SceneDelegate sceneDidDisconnect  \n  \(String(describing: window))")
    }
    
    func sceneDidBecomeActive(_ scene: UIScene) {
        //print("SceneDelegate sceneDidBecomeActive  \n  \(String(describing: window))")
    }
    
    func sceneWillResignActive(_ scene: UIScene) {
        //print("SceneDelegate sceneWillResignActive  \n  \(String(describing: window))")
    }
    
    func sceneWillEnterForeground(_ scene: UIScene) {
        //print("SceneDelegate sceneWillEnterForeground \n  \(String(describing: window))")
    }
    
    func sceneDidEnterBackground(_ scene: UIScene) {
        //print("SceneDelegate sceneDidEnterBackground  \n  \(String(describing: window))")
    }
    
    
    //RD
    @objc func loginSuccess(){
        DDLogInfo("login success")
        restoreRootViewController(RDMainTabBarViewComtroller())
    }
    
    @objc func quitSuccess(){
        DDLogInfo("quit success")
        restoreRootViewController(UINavigationController(rootViewController: SplashViewController()))
    }
    
    private func restoreRootViewController(_ rootVC : UIViewController ){
        guard let window  = self.window else {
            return
        }
        UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve) {
            DDLogInfo(" switch 1")
            let old =  UIView.areAnimationsEnabled
            UIView.setAnimationsEnabled(true)
            window.rootViewController = rootVC;
            UIView.setAnimationsEnabled(old)
        } completion: { isCompletion in
            DDLogInfo(" switch  completion = \(isCompletion)")
        }
    }
    
}

